package com.daffodil.storage.util;

import java.util.Arrays;
import java.util.Enumeration;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedCaseInsensitiveMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.MultiValueMapAdapter;

import com.daffodil.storage.exception.BaseException;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

/**
 * -对象存储通用工具类
 * @author yweijian
 * @date 2022年12月13日
 * @version 2.0.0
 * @description
 */
public class StorageUtils {

    /**
     * -上传文件后缀校验
     * @param suffix
     * @param allowSuffixs
     */
    public static void verifyAllowSuffix(String suffix, String[] allowSuffixs) {
        if(ObjectUtil.isEmpty(allowSuffixs)) {
            return;
        }
        if(ObjectUtil.isNotEmpty(allowSuffixs) && ArrayUtil.contains(allowSuffixs, suffix)) {
            return;
        }else {
            throw new BaseException(18005);
        }
    };

    /**
     * -上传文件内容类型校验
     * @param contentType
     * @param allowContentTypes
     */
    public static void verifyAllowContentType(String contentType, String[] allowContentTypes) {
        if(ObjectUtil.isEmpty(allowContentTypes)) {
            return;
        }
        if(ObjectUtil.isNotEmpty(allowContentTypes) && ArrayUtil.contains(allowContentTypes, contentType)) {
            return;
        }else {
            throw new BaseException(18005);
        }
    };

    /**
     * -上传文件大小校验
     * @param size
     * @param min
     * @param max
     */
    public static void verifyAllowSize(long size, long min, long max) {
        if(min <= 0 && max <= 0) {
            return;
        }
        if(size >= min && size <= max) {
            return;
        }else {
            throw new BaseException(18006, min, max);
        }
    }

    /**
     * -判断是否合法的业务文件夹名
     * @param folder
     */
    public static void verifyAllowFolder(String folder) {
        if(ObjectUtil.isEmpty(folder)) {
            return;
        }
        if(folder.matches("^[\\u4e00-\\u9fa5\\w]+[_\\-\\u4e00-\\u9fa5\\w\\s]*$")) {
            return;
        }else {
            throw new BaseException(18008);
        }
    }

    /**
     * -判断是否合法的对象存储key值
     * @param key
     */
    public static void verifyAllowKey(String key) {
        if(ObjectUtil.isEmpty(key)) {
            throw new BaseException(18007);
        }
        if(key.matches("^[\\u4e00-\\u9fa5\\w]+[/_\\-\\.\\u4e00-\\u9fa5\\w\\s]*$")) {
            return;
        }else {
            throw new BaseException(18007);
        }
    }

    /**
     * -获取查询对象过滤前缀
     * @param folder
     * @param name
     * @return
     */
    public static String getPrefix(String folder, String name) {
        StringBuilder prefix = new StringBuilder();
        if(ObjectUtil.isEmpty(folder)) {
            prefix.append(StrUtil.SLASH);
            if(ObjectUtil.isNotEmpty(name)) {
                prefix.append(name);
            }
        }
        if(ObjectUtil.isNotEmpty(folder)) {
            if(folder.startsWith(StrUtil.SLASH)) {
                folder = folder.substring(1);
            }
            prefix.append(folder);
            if(!folder.endsWith(StrUtil.SLASH)) {
                prefix.append(StrUtil.SLASH);
            }
            if(ObjectUtil.isNotEmpty(name)) {
                prefix.append(name);
            }
        }
        return prefix.toString();
    }
    
    /**
     * -将参数parameter转成MultiValueMap
     * @param request
     * @return
     */
    public static MultiValueMap<String, String> getParameterMap(HttpServletRequest request){
        Enumeration<String> params = request.getParameterNames();
        MultiValueMap<String, String> map = CollectionUtils.toMultiValueMap(new LinkedCaseInsensitiveMap<>(8, Locale.ENGLISH));
        if(null != params) {
            while (params.hasMoreElements()) {
                String key = params.nextElement();
                String[] value = request.getParameterValues(key);
                map.put(key, Arrays.asList(value));
            }
        }
        return new MultiValueMapAdapter<String, String>(map);
    }

}
